﻿using RecruitStudents.Client.DAL;
using RecruitStudents.Utility;
using Hxj.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RecruitStudents.Utility.Model;
using System.IO;
using System.Drawing;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLAppointment
    {
        DALAppointment dal = new DALAppointment();
        #region First
        public JsonMessage SaveAppointmentIdCard_First(string strSignupID, string strPicData)
        {
            JsonMessage message = new JsonMessage();
            var model = dal.GetModel<TB_SignUpStudentsPhoto>(TB_SignUpStudentsPhoto._.SignUpStudentsID == strSignupID);
            if (model == null)
            {
                byte[] buffer = Convert.FromBase64String(strPicData);
                var photoName = DateTime.Now.ToString("yyyyMMddhhmmssff") + ".bmp";
                model = new TB_SignUpStudentsPhoto();
                model.SignUpStudentsID = strSignupID;
                model.Photo = buffer;
                model.PhotoSize = buffer.Length.ToString();
                model.PhotoSuffix = "bmp";
                model.PhotoName = photoName;
                try
                {
                    dal.Save<TB_SignUpStudentsPhoto>(model);
                    string saveFileName = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "/Images/" + photoName;
                    if (System.IO.File.Exists(saveFileName))
                    {
                        System.IO.File.Delete(saveFileName);
                    }
                    using (MemoryStream buf = new MemoryStream(buffer))
                    {
                        Image image = Image.FromStream(buf, true);
                        image.Save(saveFileName);
                    }
                    message.State = "true";
                    message.Msg = "保存成功!";
                }
                catch (Exception ex)
                {
                    message.State = "false";
                    message.Msg = "保存失败," + ex.Message;
                }
            }
            return message;
        }

        public DataTable GetCheckStudentAppointmentDataTable_First_New(string idCard, string examPlace, string examLevel, string strExamYear)
        {
            string strSql = @"
                    select a.ExamYear, a.SignUpID,a.Name,a.NMT_SignUpID,a.IdentityCard,a.Sex,b.SpecialityKindsNameAS as SpecialityKindsName,b.SpecialityKindsID from TB_SignUpStudents as a
                    left join (
                        select distinct c.ExamPlaceNO, b.SpecialityKindsID, b.SpecialityKindsNameAS, c.SignUpID from TB_Sys_SpecialityKinds as b 
                        left join TB_Exam_SignUpStudentsSpeciality as c on b.SpecialityKindsNO = c.SpecialityKindsNO 
                        where c.ExamLevel='{1}' and c.ExamPlaceNO='{2}' and c.ExamYear='{3}' and ISNULL(c.AllowExamID,'')=''
                    ) as b on a.SignUpID = b.SignUpID where a.IdentityCard='{0}' and a.ExamYear='{3}' 
                ";
            strSql = string.Format(strSql, idCard, examLevel, examPlace, strExamYear);
            return dal.GetTable(strSql);
        }

        public TB_Sys_ExamPlaceSpecialityKindsGroupMapping GetAppointmentGroupTypeName_First(string strGroupNO)
        {
            return dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNO);
        }

        public DataTable CheckStudentAppointmentDataTable_First_New(string idCard, string examPlace, string specialityKindsNo, string examLevel, string strExamYear)
        {
            return dal.CheckStudentAppointmentDataTable_New(idCard, examPlace, specialityKindsNo, examLevel, strExamYear);
        }

        public JsonMessage MakeAppointment_First_New(string strSignUpID, string strExamPlaceNO, string strSpecNo, string strExamLevel, string strExamYear)
        {
            JsonMessage message = new JsonMessage();
            try
            {
                List<TB_Sys_ExamPlaceSpecialityKindsGroupMapping> listGroup = new List<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>();
                List<TB_Exam_SignUpStudentsSpeciality> list = dal.GetList<TB_Exam_SignUpStudentsSpeciality>(TB_Exam_SignUpStudentsSpeciality._.SignUpID == strSignUpID
                    && TB_Exam_SignUpStudentsSpeciality._.ExamPlaceNO == strExamPlaceNO
                    && TB_Exam_SignUpStudentsSpeciality._.ExamLevel == strExamLevel
                    && TB_Exam_SignUpStudentsSpeciality._.ExamYear == strExamYear
                    && TB_Exam_SignUpStudentsSpeciality._.SpecialityKindsNO.Contain(strSpecNo)
                    );
                string strAppNo = string.Empty;
                //if (list.Count > 0)
                //{
                //    strAppNo = dal.CreateExamAllowExamIdData(strExamPlaceNO, strSpecNo, list[0].ExamYear);
                //    strAppNo = strSpecNo + "" + strAppNo.PadLeft(3, '0');
                //}
                Dictionary<string, string> dicAllowExam = new Dictionary<string, string>();
                DateTime dtNow = DateTime.Now;
                foreach (TB_Exam_SignUpStudentsSpeciality model in list)
                {
                    strAppNo = string.Empty;
                    TB_Sys_SpecialityKinds modelSpec = dal.GetModel<TB_Sys_SpecialityKinds>(TB_Sys_SpecialityKinds._.SpecialityKindsNO == model.SpecialityKindsNO);
                    if (modelSpec != null)
                    {
                        if (dicAllowExam.ContainsKey(modelSpec.SpecialityKindsID))
                        {
                            strAppNo = dicAllowExam[modelSpec.SpecialityKindsID];
                        }
                        else
                        {
                            string strExamYear_New = modelSpec.ExamYear;
                            strExamYear_New = strExamYear.Substring(strExamYear_New.Length - 2);
                            strAppNo = dal.CreateExamAllowExamIdData(strExamPlaceNO, modelSpec.SpecialityKindsID, modelSpec.ExamYear);
                            if (modelSpec.SpecialityKindsID == "1303010")
                            {
                                strAppNo = strExamYear_New + "9" + strAppNo.PadLeft(4, '0');
                            }
                            else if (modelSpec.SpecialityKindsID == "1303090")
                            {
                                strAppNo = strExamYear_New + "0" + strAppNo.PadLeft(4, '0');
                            }
                            else
                            {
                                strAppNo = strExamYear_New + modelSpec.SpecialityKindsID + strAppNo.PadLeft(4, '0');
                            }
                            dicAllowExam.Add(modelSpec.SpecialityKindsID, strAppNo);
                        }
                    }
                    else
                    {
                        throw new Exception("没有找到相对应的专业，请核查报考专业！");
                    }
                    if (string.IsNullOrEmpty(model.AllowExamID))
                    {
                        model.AllowExamID = strAppNo;
                    }
                    if (ConstExamLevel.CS_Value.ToSafeString().Equals(strExamLevel))
                    {
                        var appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.SpecialityKindsNO == model.SpecialityKindsNO
                            && TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.ExamPlaceNO == strExamPlaceNO
                            && TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.ExamLevel == strExamLevel
                            && TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.ExamYear == strExamYear);
                        if (appointment != null)
                        {
                            appointment.ActualAcount = (appointment.ActualAcount ?? 0) + 1;
                            model.AppointmentGroup_First = appointment.GroupNo;
                            model.AppointmentGroupTime_First = dtNow;
                            listGroup.Add(appointment);
                        }
                    }
                }
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    foreach (TB_Exam_SignUpStudentsSpeciality model in list)
                    {
                        dal.Save<TB_Exam_SignUpStudentsSpeciality>(model, trans);
                    }
                    foreach (TB_Sys_ExamPlaceSpecialityKindsGroupMapping model in listGroup)
                    {
                        dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(model, trans);
                    }

                    trans.Commit();
                    message.State = "true";
                    message.Msg = "分配成功！";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "分配失败:" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = "分配失败:" + ex.Message;
            }
            return message;
        }

        /// <summary>
        /// 获取考点-专业-考试时间映射表（分组）信息
        /// </summary>
        /// <param name="examPlaceNo"></param>
        /// <param name="specialityKindsNo"></param>
        /// <param name="examLevel"></param>
        /// <returns></returns>
        public DataTable GetAppointmentTypeDataTable_First(string examPlaceNo, string specialityKindsNo, string examLevel)
        {
            var where = TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.ExamPlaceNO == examPlaceNo
                        && TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.SpecialityKindsNO == specialityKindsNo
                        && TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.ExamLevel == examLevel;

            DataTable dt = dal.GetTable<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(where);

            foreach (DataRow dr in dt.Rows)
            {
                dr["ActualAcount"] = dal.GetAppointmentStudentCount(dr["GroupNo"].ToSafeString(), examPlaceNo, specialityKindsNo, examLevel);
            }
            return dt;
        }


        public DataTable GetAppointmentStudentDataTable_First_New(string examPlaceNo, string specialityKindsNo, string examLevel,
            string strExamYear, string strName, string strIdCard)
        {
            //string strSql = "select distinct a.SignUpID,a.Name,a.IdentityCard,b.AppointmentGroupTime_First,b.AllowExamID from TB_SignUpStudents as a left join TB_Exam_SignUpStudentsSpeciality as b on a.SignUpID=b.SignUpID where b.ExamPlaceNO = '{0}' and b.ExamLevel = '{1}' and ISNULL(b.AppointmentGroupTime_First, '')<> '' and b.SpecialityKindsNO like '{2}%'";

            //strSql = string.Format(strSql, examPlaceNo, examLevel, specialityKindsNo);
            string strSql = @"select distinct a.SignUpID,a.Name,a.IdentityCard,b.AppointmentGroupTime_First,b.AllowExamID,c.SpecialityKindsNameAS,c.SpecialityKindsID 
                 from TB_SignUpStudents as a left join TB_Exam_SignUpStudentsSpeciality as b on a.SignUpID = b.SignUpID
                left join TB_Sys_SpecialityKinds as c on c.SpecialityKindsNO = b.SpecialityKindsNO
                where b.ExamPlaceNO = '{0}' and b.ExamLevel = '{1}' and b.ExamYear='{2}' and ISNULL(b.AllowExamID, '')<> '' {3}
                order by b.AppointmentGroupTime_First desc
            ";
            string strWhere = "";
            if (!string.IsNullOrEmpty(strName))
            {
                strWhere += " and a.Name like '%" + strName + "%' ";
            }
            if (!string.IsNullOrEmpty(strIdCard))
            {
                strWhere += " and a.IdentityCard ='" + strIdCard + "'";
            }
            strSql = string.Format(strSql, examPlaceNo, examLevel, strExamYear, strWhere);
            return dal.GetTable(strSql);
        }


        /// <summary>
        /// 获取组内学生
        /// </summary>
        /// <param name="examPlaceSpecialityKindsExamTimeMappingId"></param>
        /// <param name="examPlaceNo"></param>
        /// <param name="specialityKindsNo"></param>
        /// <param name="examLevel"></param>
        /// <returns></returns>
        public DataTable GetAppointmentStudentDataTable_First(string strGroupNo, string examPlaceNo, string specialityKindsNo, string examLevel)
        {
            return dal.GetAppointmentStudentDataTable(strGroupNo, examPlaceNo, specialityKindsNo, examLevel);
        }

        public DataSet GetStudentAppointmentDataTable_Print(string idCard, string examPlace, string specialityKindsNo, string examLevel, string strExamYear)
        {
            return dal.GetStudentAppointmentDataTable_Print(idCard, examPlace, specialityKindsNo, examLevel, strExamYear);
        }

        public DataTable CheckStudentAppointmentDataTable_First(string idCard, string examPlace, string specialityKindsNo, string examLevel)
        {
            return dal.CheckStudentAppointmentDataTable(idCard, examPlace, specialityKindsNo, examLevel);
        }

        public JsonMessage MakeAppointment_First(string strSignUpStudentsSpecialityID, string strGroupNo)
        {
            JsonMessage message = new JsonMessage();
            var appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
            var model = dal.GetModel<TB_Exam_SignUpStudentsSpeciality>(TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID == strSignUpStudentsSpecialityID);
            if (appointment != null && model != null)
            {
                string strAppNo = dal.CreateExamAllowExamIdData(model.ExamPlaceNO, "1303090", model.ExamYear);
                strAppNo = "BY" + strAppNo.PadLeft(3, '0');
                if (string.IsNullOrEmpty(model.AllowExamID))
                {
                    model.AllowExamID = strAppNo;
                }
                var strag = "";
                if (ConstExamLevel.CS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_First;
                    model.AppointmentGroup_First = appointment.GroupNo;
                    model.AppointmentGroupTime_First = DateTime.Now;
                }
                else if (ConstExamLevel.FS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Second;
                    model.AppointmentGroup_Second = appointment.GroupNo;
                    model.AppointmentGroupTime_Second = DateTime.Now;
                }
                else if (ConstExamLevel.SS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Third;
                    model.AppointmentGroup_Third = appointment.GroupNo;
                    model.AppointmentGroupTime_Third = DateTime.Now;
                }
                else if (ConstExamLevel.SSS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fourth;
                    model.AppointmentGroup_Fourth = appointment.GroupNo;
                    model.AppointmentGroupTime_Fourth = DateTime.Now;
                }
                else if (ConstExamLevel.WWS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fifth;
                    model.AppointmentGroup_Fifth = appointment.GroupNo;
                    model.AppointmentGroupTime_Fifth = DateTime.Now;
                }
                else if (ConstExamLevel.LLS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Sixth;
                    model.AppointmentGroup_Sixth = appointment.GroupNo;
                    model.AppointmentGroupTime_Sixth = DateTime.Now;
                }
                appointment.ActualAcount = (appointment.ActualAcount ?? 0) + 1;
                model.ExamLevel = Convert.ToInt32(appointment.ExamLevel);
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    dal.Save<TB_Exam_SignUpStudentsSpeciality>(model, trans);
                    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointment, trans);
                    trans.Commit();
                    message.State = "true";
                    message.Msg = "分配成功！";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "分配失败:" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            else
            {
                message.State = "false";
                message.Msg = "分配失败:无分组信息";
            }
            return message;
        }

        public JsonMessage CancelAppointment_First(string strSignUpStudentsSpecialityID, string strGroupNo)
        {
            JsonMessage message = new JsonMessage();
            var appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
            var model = dal.GetModel<TB_Exam_SignUpStudentsSpeciality>(TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID == strSignUpStudentsSpecialityID);
            if (appointment != null)
            {
                var strag = "";
                if (ConstExamLevel.CS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_First;
                    model.AppointmentGroup_First = "";
                    model.AppointmentGroupTime_First = null;
                }
                else if (ConstExamLevel.FS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Second;
                    model.AppointmentGroup_Second = "";
                    model.AppointmentGroupTime_Second = null;
                }
                else if (ConstExamLevel.SS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Third;
                    model.AppointmentGroup_Third = "";
                    model.AppointmentGroupTime_Third = null;
                }
                else if (ConstExamLevel.SSS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fourth;
                    model.AppointmentGroup_Fourth = "";
                    model.AppointmentGroupTime_Fourth = null;
                }
                else if (ConstExamLevel.WWS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fifth;
                    model.AppointmentGroup_Fifth = "";
                    model.AppointmentGroupTime_Fifth = null;
                }
                else if (ConstExamLevel.LLS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Sixth;
                    model.AppointmentGroup_Sixth = "";
                    model.AppointmentGroupTime_Sixth = null;
                }
                if (!string.IsNullOrEmpty(strag))
                {
                    appointment.ActualAcount = (appointment.ActualAcount ?? 0) - 1 >= 0 ? (appointment.ActualAcount ?? 0) - 1 : 0;
                    //model.ExamLevel = Convert.ToInt32(appointment.ExamLevel) - 1 >= 0 ? Convert.ToInt32(appointment.ExamLevel) - 1 : 0;
                }
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    dal.Save<TB_Exam_SignUpStudentsSpeciality>(model, trans);
                    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointment, trans);
                    trans.Commit();
                    message.State = "true";
                    message.Msg = "取消成功！";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "取消成功:" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            return message;
        }
        #endregion

        #region Second
        public DataTable GetAppointmentStudentDataTable_Second(string strGroupNo, string examPlaceNo, string specialityKindsNo, string examLevel)
        {
            return dal.GetAppointmentStudentDataTable(strGroupNo, examPlaceNo, specialityKindsNo, examLevel);
        }

        public DataTable CheckStudentAppointmentDataTable_Second(string strIdCard, string strExamPlaceID, string strSpecialityKindsNO, string strExamLevel)
        {
            var where = TB_Exam_SignUpStudentsSpeciality._.ExamPlaceNO == strExamPlaceID
                       && TB_Exam_SignUpStudentsSpeciality._.SpecialityKindsNO.SelectIn(strSpecialityKindsNO.Split(','))
                       && TB_Exam_SignUpStudentsSpeciality._.IsActive == "1"
                       && TB_SignUpStudents._.IdentityCard == strIdCard;
            var selectField = new List<Hxj.Data.Field>();
            selectField.Add(TB_Exam_SignUpStudentsSpeciality._.All);
            selectField.Add(TB_SignUpStudents._.Name);
            selectField.Add(TB_SignUpStudents._.ExamProvince);
            selectField.Add(TB_SignUpStudents._.IdentityCard);
            selectField.Add(TB_SignUpStudents._.NMT_SignUpID);
            selectField.Add(TB_SignUpStudents._.LinkMobile);
            where = where.And(TB_Exam_SignUpStudentsSpeciality._.ExamLevel == strExamLevel);
            switch (strExamLevel.ToSafeInt())
            {
                case ConstExamLevel.CS_Value:
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_First.As("AppointmentGroup"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_First.As("AppointmentGroupTime"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_First.As("IsFee"));
                    break;
                case ConstExamLevel.FS_Value:
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Second.As("AppointmentGroup"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Second.As("AppointmentGroupTime"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Second.As("IsFee"));
                    break;
                case ConstExamLevel.SS_Value:
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Third.As("AppointmentGroup"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Third.As("AppointmentGroupTime"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Third.As("IsFee"));
                    break;
                case ConstExamLevel.SSS_Value:
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Fourth.As("AppointmentGroup"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Fourth.As("AppointmentGroupTime"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Fourth.As("IsFee"));
                    break;
                case ConstExamLevel.WWS_Value:
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Fifth.As("AppointmentGroup"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Fifth.As("AppointmentGroupTime"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Fifth.As("IsFee"));
                    break;
                case ConstExamLevel.LLS_Value:
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Sixth.As("AppointmentGroup"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Sixth.As("AppointmentGroupTime"));
                    selectField.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Sixth.As("IsFee"));
                    break;
                default:
                    where = where.And(TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID.IsNull());
                    break;
            }

            return DbHelper.GetDatabase().From<TB_Exam_SignUpStudentsSpeciality>() //选择表
                                         .LeftJoin<TB_SignUpStudents>(TB_SignUpStudents._.SignUpID == TB_Exam_SignUpStudentsSpeciality._.SignUpID)
                                         .Select(selectField.ToArray())  //设置选择的字段数目 
                                         .Where(where)   //条件
                                         .ToDataTable();   //返回List
        }

        public JsonMessage MakeAppointment_Second(string strSignUpStudentsSpecialityID, string strGroupNo)
        {
            JsonMessage message = new JsonMessage();
            var appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
            var model = dal.GetModel<TB_Exam_SignUpStudentsSpeciality>(TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID == strSignUpStudentsSpecialityID);
            if (appointment != null)
            {
                var strag = "";
                if (ConstExamLevel.CS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_First;
                    model.AppointmentGroup_First = appointment.GroupNo;
                    model.AppointmentGroupTime_First = DateTime.Now;
                }
                else if (ConstExamLevel.FS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Second;
                    model.AppointmentGroup_Second = appointment.GroupNo;
                    model.AppointmentGroupTime_Second = DateTime.Now;
                }
                else if (ConstExamLevel.SS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Third;
                    model.AppointmentGroup_Third = appointment.GroupNo;
                    model.AppointmentGroupTime_Third = DateTime.Now;
                }
                else if (ConstExamLevel.SSS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fourth;
                    model.AppointmentGroup_Fourth = appointment.GroupNo;
                    model.AppointmentGroupTime_Fourth = DateTime.Now;
                }
                else if (ConstExamLevel.WWS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fifth;
                    model.AppointmentGroup_Fifth = appointment.GroupNo;
                    model.AppointmentGroupTime_Fifth = DateTime.Now;
                }
                else if (ConstExamLevel.LLS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Sixth;
                    model.AppointmentGroup_Sixth = appointment.GroupNo;
                    model.AppointmentGroupTime_Sixth = DateTime.Now;
                }
                appointment.ActualAcount = (appointment.ActualAcount ?? 0) + 1;
                //model.ExamLevel = Convert.ToInt32(appointment.ExamLevel);
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    dal.Save<TB_Exam_SignUpStudentsSpeciality>(model, trans);
                    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointment, trans);
                    trans.Commit();
                    message.State = "true";
                    message.Msg = "分配成功！";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "分配失败:" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            else
            {
                message.State = "false";
                message.Msg = "分配失败:无分组信息";
            }
            return message;
        }

        public JsonMessage SaveAppointmentIdCard_Second(string signupid, string strPicData)
        {
            JsonMessage message = new JsonMessage();
            var model = dal.GetModel<TB_SignUpStudentsPhoto>(TB_SignUpStudentsPhoto._.SignUpStudentsID == signupid);
            if (model == null)
            {
                byte[] buffer = Convert.FromBase64String(strPicData);
                var photoName = DateTime.Now.ToString("yyyyMMddhhmmssff") + ".bmp";
                model = new TB_SignUpStudentsPhoto();
                model.SignUpStudentsID = signupid;
                model.Photo = buffer;
                model.PhotoSize = buffer.Length.ToString();
                model.PhotoSuffix = "bmp";
                model.PhotoName = photoName;
                try
                {
                    dal.Save<TB_SignUpStudentsPhoto>(model);
                    string saveFileName = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "/Images/" + photoName;
                    if (System.IO.File.Exists(saveFileName))
                    {
                        System.IO.File.Delete(saveFileName);
                    }
                    using (MemoryStream buf = new MemoryStream(buffer))
                    {
                        Image image = Image.FromStream(buf, true);
                        image.Save(saveFileName);
                    }
                    message.State = "true";
                    message.Msg = "保存成功!";
                }
                catch (Exception ex)
                {
                    message.State = "false";
                    message.Msg = "保存失败," + ex.Message;
                }
            }
            return message;
        }

        public JsonMessage CancelAppointment_Second(string strSignUpStudentsSpecialityID, string strGroupNo)
        {
            JsonMessage message = new JsonMessage();
            var appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
            var model = dal.GetModel<TB_Exam_SignUpStudentsSpeciality>(TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID == strSignUpStudentsSpecialityID);
            if (appointment != null)
            {
                var strag = "";
                if (ConstExamLevel.CS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_First;
                    model.AppointmentGroup_First = "";
                    model.AppointmentGroupTime_First = null;
                }
                else if (ConstExamLevel.FS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Second;
                    model.AppointmentGroup_Second = "";
                    model.AppointmentGroupTime_Second = null;
                }
                else if (ConstExamLevel.SS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Third;
                    model.AppointmentGroup_Third = "";
                    model.AppointmentGroupTime_Third = null;
                }
                else if (ConstExamLevel.SSS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fourth;
                    model.AppointmentGroup_Fourth = "";
                    model.AppointmentGroupTime_Fourth = null;
                }
                else if (ConstExamLevel.WWS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Fifth;
                    model.AppointmentGroup_Fifth = "";
                    model.AppointmentGroupTime_Fifth = null;
                }
                else if (ConstExamLevel.LLS_Value.Equals(appointment.ExamLevel))
                {
                    strag = model.AppointmentGroup_Sixth;
                    model.AppointmentGroup_Sixth = "";
                    model.AppointmentGroupTime_Sixth = null;
                }
                if (!string.IsNullOrEmpty(strag))
                {
                    appointment.ActualAcount = (appointment.ActualAcount ?? 0) - 1 >= 0 ? (appointment.ActualAcount ?? 0) - 1 : 0;
                    model.ExamLevel = Convert.ToInt32(appointment.ExamLevel) - 1 >= 0 ? Convert.ToInt32(appointment.ExamLevel) - 1 : 0;
                }
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    dal.Save<TB_Exam_SignUpStudentsSpeciality>(model, trans);
                    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointment, trans);
                    trans.Commit();
                    message.State = "true";
                    message.Msg = "取消成功！";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "取消失败:" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            return message;
        }
        #endregion

    }
}
